/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbAdpTanx-1.0.0		*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_01			*/
/*	File:		GlbAdpTanx.h			*/
/********************************************************/

#ifndef	_GLBADPTANX_H
#define	_GLBADPTANX_H

#include "GlbMd5.h"
#include "GlbSet.h"
#include "GlbPro.h"
#include "GlbTcp.h"
#include "GlbUdp.h"
#include "GlbUtil.h"
#include "GlbConf.h"
#include "GlbFile.h"
#include "GlbPool.h"
#include "GlbTime.h"
#include "GlbEpoll.h"

#include "GlbAdpDef.h"
#include "GlbRtbDef.h"
#include "tanx-bidding.pb.h"

#include <zlib.h>
#include <getopt.h>
#include <signal.h>
#include <iostream>

#define	GLB_AT_KVS			64
#define	GLB_AT_SET			0x10

#define	GLB_AT_COMP			0x01

#define	GLB_AT_DEV			const_cast<char*>("DEV")
#define	GLB_AT_SEC			const_cast<char*>("USEC")
#define	GLB_AT_GLB			const_cast<char*>("GLOBAL")
#define	GLB_AT_REQ			const_cast<char*>("REQLOG")
#define	GLB_AT_RES			const_cast<char*>("RESLOG")
#define	GLB_AT_LOG			const_cast<char*>("LOGFILE")
#define	GLB_AT_ENG			const_cast<char*>("GLBENGINE")

#define	GLB_AT_POST			const_cast<char*>("POST")
#define	GLB_AT_RNRN			const_cast<char*>("\r\n\r\n")
#define	GLB_AT_LENG			const_cast<char*>("Content-Length: ")

#define	GLB_AT_INFO			"orderID:%s\texecuteID:%s\tcreativeID:%s\tmaterialID:%s\ttID:%s\trequestID:%s\tunionID:%s\tbidding_price:%s\tres_equip_id:%s\tpID:%s"
#define	GLB_AT_SNIP			"<iframe width=\"%d\" height=\"%d\" frameborder=\"0\" scrolling=\"no\" src=\"%s?p=%%%%SETTLE_PRICE%%%%&pid=%s&bid=%s&info=%s&tanxclick=%%%%CLICK_URL%%%%\"></iframe>"

#define	GLB_AT_VERSION			const_cast<char*>("GlbAdpTanx-1.0.0")
#define	GLB_AT_HTTP200			const_cast<char*>("HTTP/1.0 200 OK\r\nContent-Length: %d\r\nContent-Type: text/html; charset=UTF-8\r\nServer: TornadoServer/2.2\r\n\r\n")

#define	GLB_AT_TTLADD			stHead.m_usTotal += 1
#define	GLB_AT_VALCLS			memset(szVal, '\0', GLB_KBYTES1)

#define	GLB_AT_STRING(K, V)\
{\
	GLB_AT_TTLADD;\
	stKv.m_ucKey = K;\
	stKv.m_iLength = V.length();\
	stKv.m_puszValue = (UCHAR*)V.c_str();\
	iComp += GlbProKvOutput(&stKv, GLB_PRO_1, uszComp + iComp, GLB_KBYTES4 - iComp);\
}

#define	GLB_AT_UINT32(K, V)\
{\
	GLB_AT_TTLADD;\
	GLB_AT_VALCLS;\
	stKv.m_ucKey = K;\
	sprintf(szVal, "%d", V);\
	stKv.m_iLength = strlen(szVal);\
	stKv.m_puszValue = (UCHAR*)szVal;\
	iComp += GlbProKvOutput(&stKv, GLB_PRO_1, uszComp + iComp, GLB_KBYTES4 - iComp);\
}

#define	GLB_AT_VALUE(V)\
{\
	V[stKv[usCount].m_iLength] = '\0';\
	memcpy(V, stKv[usCount].m_puszValue, stKv[usCount].m_iLength);\
}

typedef struct tagGlbAtEng
{
	char m_szDev[GLB_BYTE16];	// dev
	char m_szUrl[GLB_BYTE32];	// url
}GLBATENG_S;

typedef struct tagGlbAtPara
{
	int m_iSocket;			// Socket
	int m_iLength;			// Length
	UCHAR* m_puszData;		// Buffer
	GLBADPMSG_S m_stReq;		// ReqLog
	GLBADPMSG_S m_stRes;		// ResLog
	char m_szVer[GLB_BYTE16];	// Version
	char m_szBid[GLB_BYTE64];	// TanxBid
}GLBATPARA_S;

typedef struct tagGlbAtInfo
{
	char m_szOID[GLB_BYTE32];	// order_id
	char m_szEID[GLB_BYTE32];	// execute_id
	char m_szCID[GLB_BYTE32];	// creative_type
	char m_szMID[GLB_BYTE32];	// material_id
	char m_szUID[GLB_BYTE64];	// user id
	char m_szBID[GLB_BYTE64];	// bid
	char m_szLID[GLB_BYTE32];	// league id
	char m_szMcp[GLB_BYTE32];	// min_cpm_price
	char m_szDev[GLB_BYTE32];	// region
	char m_szPID[GLB_BYTE64];	// pid
	char m_szSiz[GLB_BYTE32];	// size
	char m_szSnp[GLB_KBYTES2];	// snippet
}GLBATINFO_S;

typedef struct tagGlbAtConf
{
	char m_szDev[GLB_BYTE16];
	char m_szUsec[GLB_BYTE16];
	char m_szLogReq[GLB_BYTE512];
	char m_szLogRes[GLB_BYTE512];
	GlbCls::CGlbSet m_CEng;
}GLBATCONF_S;

typedef Tanx::BidRequest		GLBATREQ_C;
typedef Tanx::BidResponse		GLBATRES_C;
typedef Tanx::BidResponse_Ads		GLBATRESAD_C;
typedef Tanx::BidRequest_AdzInfo	GLBATREQAD_C;

void GlbAdpTanxCfgPrint();
void GlbAdpTanxQuit(int iSignal);
int GlbAdpTanxCfgLoad(char* pszConf);
int GlbAdpTanxEpollCore(int iSocket);
void* GlbAdpTanxPoolCore(void* pPara);
void* GlbAdpTanxFree(GLBATPARA_S* pstPara);
void GlbAdpTanxEmpty(GLBATPARA_S* pstPara);
void GlbAdpTanxMsgLog(GLBADPMSG_S* pstMsg);
GLBATPARA_S* GlbAdpTanxMalloc(int iLength);
int GlbAdpTanxParse(GLBATPARA_S* pstPara);
int GlbAdpTanxRtb(int iHash, GLBATPARA_S* pstPara);
void GlbAdpTanxAdSize(char* pszSize, UINT* puiWid, UINT* puiHei);

inline int GlbAdpTanxCompress(UCHAR* puszSrc, int iSrc, UCHAR* puszDst, int* piDst)
{
	*(ULONG*)piDst = GLB_KBYTES4;

	return compress(puszDst, (ULONG*)piDst, (const Bytef*)puszSrc, (ULONG)iSrc);
}

inline int GlbAdpTanxUnCompress(UCHAR* puszSrc, int iSrc, UCHAR* puszDst, int* piDst)
{
	*(ULONG*)piDst = GLB_KBYTES4;

	return uncompress(puszDst, (ULONG*)piDst, (const Bytef*)puszSrc, (ULONG)iSrc);
}

#endif /* _GLBADPTANX_H */
